package com.ruoyi.web.controller.biz;

import java.util.List;

import com.ruoyi.biz.domain.BizCourse;
import com.ruoyi.biz.domain.BizSubject;
import com.ruoyi.biz.service.IBizCourseService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.biz.service.IBizSubjectService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.domain.Ztree;

/**
 * 学科管理Controller
 * 
 * @author ruoyi
 * @date 2021-08-07
 */
@Controller
@RequestMapping("/biz/subject")
public class BizSubjectController extends BaseController
{
    private String prefix = "biz/subject";

    @Autowired
    private IBizSubjectService bizSubjectService;
    @Autowired
    private IBizCourseService courseService;

    @RequiresPermissions("biz:subject:view")
    @GetMapping()
    public String manage()
    {
        return prefix + "/manage";
    }

    /**
     * 查询学科管理树列表
     */
    @RequiresPermissions("biz:subject:list")
    @PostMapping("/list")
    @ResponseBody
    public List<BizSubject> list(BizSubject bizSubject)
    {
        List<BizSubject> list = bizSubjectService.selectBizSubjectList(bizSubject);
        return list;
    }

    /**
     * 导出学科管理列表
     */
    @RequiresPermissions("biz:subject:export")
    @Log(title = "学科管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(BizSubject bizSubject)
    {
        List<BizSubject> list = bizSubjectService.selectBizSubjectList(bizSubject);
        ExcelUtil<BizSubject> util = new ExcelUtil<BizSubject>(BizSubject.class);
        return util.exportExcel(list, "manage");
    }

    /**
     * 新增学科管理
     */
    @GetMapping(value = { "/add/{id}", "/add/" })
    public String add(@PathVariable(value = "id", required = false) Long id, ModelMap mmap)
    {
        if (StringUtils.isNotNull(id))
        {
            mmap.put("bizSubject", bizSubjectService.selectBizSubjectById(id));
        }
        return prefix + "/add";
    }

    /**
     * 新增保存学科管理
     */
    @RequiresPermissions("biz:subject:add")
    @Log(title = "学科管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(BizSubject bizSubject)
    {
        return toAjax(bizSubjectService.insertBizSubject(bizSubject));
    }

    /**
     * 修改学科管理
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        BizSubject bizSubject = bizSubjectService.selectBizSubjectById(id);
        mmap.put("bizSubject", bizSubject);
        return prefix + "/edit";
    }

    /**
     * 修改保存学科管理
     */
    @RequiresPermissions("biz:subject:edit")
    @Log(title = "学科管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(BizSubject bizSubject)
    {
        return toAjax(bizSubjectService.updateBizSubject(bizSubject));
    }

    /**
     * 删除
     */
    @RequiresPermissions("biz:subject:remove")
    @Log(title = "学科管理", businessType = BusinessType.DELETE)
    @GetMapping("/remove/{id}")
    @ResponseBody
    public AjaxResult remove(@PathVariable("id") Long id)
    {
        BizCourse course = new BizCourse();
        course.setSubjectId(id);
        List<BizCourse> bizCourses = courseService.selectBizCourseList(course);
        if (!CollectionUtils.isEmpty(bizCourses)) {
            return error("学科下有课程内容,请先删除课程内容后再操作");
        }
        return toAjax(bizSubjectService.deleteBizSubjectById(id));
    }

    /**
     * 选择学科管理树
     */
    @GetMapping(value = { "/selectManageTree/{id}", "/selectManageTree/" })
    public String selectManageTree(@PathVariable(value = "id", required = false) Long id, ModelMap mmap)
    {
        if (StringUtils.isNotNull(id))
        {
            mmap.put("bizSubject", bizSubjectService.selectBizSubjectById(id));
        }
        return prefix + "/tree";
    }

    /**
     * 加载学科管理树列表
     */
    @GetMapping("/treeData")
    @ResponseBody
    public List<Ztree> treeData()
    {
        List<Ztree> ztrees = bizSubjectService.selectBizSubjectTree();
        return ztrees;
    }
}
